package org.biloxi.model.magicobjects;

import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Map;

import org.biloxi.commands.CommandRepository;
import org.biloxi.model.magicobjects.impl.Plains;
import org.biloxi.model.magicobjects.impl.Swamp;
import org.biloxi.model.magicobjects.impl.VenomousDragonFly;

/**
 * Singleton Registry of MagicObjects.
 * In charge of retrieving Card information and creating MagicObject by card name.  
 * 
 * @author Peter.WM.Wong@gmail.com
 */
public class MagicObjectRegistry {
	private static MagicObjectRegistry _instance = null;
	private long curCardId = 0;
	
	public static MagicObjectRegistry inst(){
		if(_instance == null){
			_instance = new MagicObjectRegistry();
			
			//TODO Dynamic population
			_instance._moMap.put(Swamp.class.getSimpleName(), Swamp.class);
			_instance._moMap.put(VenomousDragonFly.class.getSimpleName(), VenomousDragonFly.class);
			_instance._moMap.put(Plains.class.getSimpleName(), Plains.class);
		}
		return _instance;
	}
	private final Map<String,Class<? extends MagicObject>> _moMap = new HashMap<String,Class<? extends MagicObject>>();
	private MagicObjectRegistry(){}
	
	//TODO: Fix this
	public MagicObject createMagicObject(CommandRepository cr, String cardid){
		MagicObject result = null;
		try{
			Class<? extends MagicObject> c = _moMap.get(cardid);
			Constructor<? extends MagicObject> con = c.getDeclaredConstructor(CommandRepository.class, long.class);
			con.setAccessible(true); //up up down down left right left right a b start
			result = con.newInstance(cr, curCardId++);
		}catch(Exception e){
			e.printStackTrace();
		}
		return result;
	}
}
